package com.project.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClassBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IRoomMapper;

import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
@Service
@Transactional
public class RoomServiceImp   implements IRoomService  {
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IStudentMapper stuMapper;
    @Override
    public ArrayList<RoomBean> showAllRooms() {
       return (ArrayList<RoomBean>) roomMapper.selectList(null);
    }
    @Override
    public ArrayList<RoomBean> findRoomByGender(String gender) {
       return roomMapper.findRoomByGender(gender);
    }

    @Override
    public boolean addRoom(RoomBean roomBean) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("r_address",roomBean.getRoomAddress());
        RoomBean room = roomMapper.selectOne(qw);
        if(room==null){
            roomMapper.insert(roomBean);
            return true;
        }
        return false;
    }

    @Override
    public boolean delRoom(Integer roomId) {
        QueryWrapper<StudentBean> qw =new QueryWrapper<>();
        qw.eq("fk_roomid",roomId);
        int inNum = stuMapper.selectCount(qw);
        if(inNum==0){
            QueryWrapper qw1 =new QueryWrapper<>();
            qw1.eq("pk_roomid",roomId);
            roomMapper.delete(qw1);
            return true;
        }
        return false;
    }

    @Override
    public RoomBean roomById(Integer roomId) {
        return roomMapper.roomById(roomId);
    }



    @Override
    public IPage<RoomBean> cutPageRoomByCondition(Integer pageNum, String roomAddress, String roomState, String roomType, String canInOrNo) {
        return  roomMapper.cutPageRoomByCondition(new Page(pageNum,2),roomAddress,roomState,roomType,canInOrNo);
    }


}
